home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 June: Reference Library / Dev.CD Jun 94.toast / Periodicals / develop / develop Issue 10 / develop 10 code / Is it Art? / ArtMaker / PaintInit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-08  |  8.0 KB  |  292 lines  |  [TEXT/KAHL]

  1. #include    "Painterly.h"
  2.  
  3. /* Painterly Globals */
  4. extern MenuHandle        gPaintMenuHandles[];         /* The menus we add */
  5. extern DocumentRecord    gSrcDoc, gDstDoc;            /* The doc records */
  6. extern CWindowPtr        gSrcWindPtr, gDstWindPtr;    /* pointers to the windows, for convenience */
  7. extern GWorldPtr        gUndoBuffer;                /* a buffer to allow undo */
  8. extern BrushParams        gBrushStuff;                /* the parameter structure for brush calls */
  9. extern THPrint            gPrintRecHandle;            /* The print record */
  10. extern short            gDocTitleHeight, gDocFrameWidth; /* Window Stats */
  11. /* Prototypes for local (to this file) routines */
  12. Boolean AddStdScrollBars(WindowPtr wind);
  13. Boolean CheckMachine(void);
  14.  
  15. /* Called by the Shell at startup time */
  16. Boolean PaintInit(void)
  17. {
  18.     MenuHandle    mhndl;
  19.     Str255        brushname, wTitle;
  20.     Rect        temprect;
  21.     short        rslt;
  22.     
  23.     /* Check for the machine characteristics we need to run */
  24.     if(CheckMachine() == false)
  25.     {
  26.         DoErrorAlert(kWimpyMachineStr, 0);
  27.         return false;
  28.     }
  29.     
  30.     /* Get the window measurements we'll need later */
  31.     CalcWindowStats();
  32.         
  33.        /* Create the document windows. They remain invisible until a document is opened */
  34.     SetRect(&temprect, 10, 10 + 20 + GetMBarHeight(), 210, 210 + 20 + GetMBarHeight());
  35.     gSrcWindPtr = NewCWindow(    (Ptr)&gSrcDoc, 
  36.                                 &temprect, 
  37.                                 TheStr(wTitle, kSrcPictStr),
  38.                                 false, 
  39.                                 zoomDocProc,
  40.                                   (WindowPtr)(-1), 
  41.                                   true,     /* Only the source window has a close box */
  42.                                   0L    );
  43.     SetPort(gSrcWindPtr);
  44.     ClipRect(&gSrcWindPtr->portRect);
  45.     EraseRect(&gSrcWindPtr->portRect);
  46.     
  47.     OffsetRect(&temprect, 32, 32);
  48.     gDstWindPtr = NewCWindow(    (Ptr)&gDstDoc, 
  49.                                 &temprect, 
  50.                                 TheStr(wTitle, kArtStr),
  51.                                 false, 
  52.                                 zoomDocProc,
  53.                                   (WindowPtr)(-1), 
  54.                                   false, 
  55.                                   0L    );
  56.     SetPort(gDstWindPtr);
  57.     ClipRect(&gDstWindPtr->portRect);
  58.     EraseRect(&gDstWindPtr->portRect);
  59.     
  60.     /* We don't create the GWorlds yet: that'll happen when the user opens a file */
  61.  
  62.     /* Set up the other Doc info */
  63.     gSrcDoc.dirty = false;
  64.     gDstDoc.dirty = false;
  65.     *gSrcDoc.fileSpec.name = 0; /* make the file name an empty string */
  66.     *gDstDoc.fileSpec.name = 0; /* make the file name an empty string */
  67.     if(!AddStdScrollBars(gSrcWindPtr) || !AddStdScrollBars(gDstWindPtr))
  68.     {
  69.         DoErrorAlert(kNoResStr, 0);
  70.         return false;
  71.     }
  72.     
  73.     /* Set up the global BrushParams */
  74.     gBrushStuff.pt.h = 0;
  75.     gBrushStuff.pt.v = 0;
  76.     gBrushStuff.rect = gSrcWindPtr->portRect;
  77.     gBrushStuff.color.red = 0;
  78.     gBrushStuff.color.green = 0;
  79.     gBrushStuff.color.blue = 0;
  80.     gBrushStuff.theSource = gSrcDoc.world;
  81.     gBrushStuff.theDestination = gDstDoc.world;
  82.     gBrushStuff.storage = (long)nil;
  83.     
  84.     /* make a print record */
  85.     gPrintRecHandle = (THPrint)NewHandle(sizeof(TPrint));
  86.     if(gPrintRecHandle != nil)
  87.     {
  88.         /* Fill it with default values */
  89.         PrOpen();
  90.         if(PrError() == noErr)
  91.         {
  92.             PrintDefault(gPrintRecHandle);
  93.             PrClose();
  94.         }
  95.     }
  96.     
  97.     /* Set up Filter menu */
  98.     mhndl = GetMenu(kFilterMenuID);
  99.     if(mhndl == nil)
  100.     {
  101.         DoErrorAlert(kNoResStr, 0);
  102.         return false;
  103.     }
  104.     (*mhndl)->menuID = kFilterMenuID;
  105.     BuildFilterMenu(mhndl);        /* Adds all available Filters to the menu */
  106.     InsertMenu(mhndl, 0);
  107.     DisableItem(mhndl, 0); /* Disable the menu until user opens a file */
  108.     gPaintMenuHandles[kFilterMenu] = mhndl;
  109.  
  110.     /* Set up brush menu */
  111.     mhndl = GetMenu(kBrushMenuID);
  112.     if(mhndl == nil)
  113.     {
  114.         DoErrorAlert(kNoResStr, 0);
  115.         return false;
  116.     }
  117.     (*mhndl)->menuID = kBrushMenuID;
  118.     BuildBrushMenu(mhndl);        /* Adds all available brushes to the menu */
  119.     InsertMenu(mhndl, 0);
  120.     DisableItem(mhndl, 0); /* Disable the menu until user opens a file */
  121.     gPaintMenuHandles[kBrushMenu] = mhndl;
  122.     
  123.     /* Load the first Brush */
  124.     rslt = SetCurrentBrush(kFirstBrush);
  125.     if(rslt != noErr)
  126.     {
  127.         DoErrorAlert(kGenericErrorStr, 0);
  128.         return false;
  129.     }
  130.     
  131.     /* Set up auto-paint menu */
  132.     mhndl = GetMenu(kAutoPaintMenuID);
  133.     if(mhndl == nil)
  134.     {
  135.         DoErrorAlert(kNoResStr, 0);
  136.         return false;
  137.     }
  138.     (*mhndl)->menuID = kAutoPaintMenuID;
  139.     InsertMenu(mhndl, 0);
  140.     DisableItem(mhndl, 0); /* Disable the menu until user opens a file */
  141.     gPaintMenuHandles[kAutoPaintMenu] = mhndl;
  142.     
  143.     /* Update the menu bar */
  144.     DrawMenuBar();
  145.     
  146.     return(true);
  147. }
  148.  
  149. /* Adds 2 scroll bars to the given window, adjusting them to fit */
  150. Boolean AddStdScrollBars(WindowPtr wind)
  151. {
  152.     ControlHandle    scrollHandle;
  153.     Rect            scrollRect;
  154.     short            min, max;
  155.     Boolean            rslt = false;
  156.     
  157.     /* These are just dummy values for the rect and the min and max. They will be 
  158.         adjusted in the AdjustScrollBars routine */
  159.     
  160.     /* First the horizontal one */
  161.     SetRect(&scrollRect, 0, 0, 10, 10);
  162.     min = 0;
  163.     max = 10;
  164.     scrollHandle = NewControl(wind, &scrollRect, "\p", false, 
  165.                 min, min, max, scrollBarProc, 0);
  166.                 
  167.     if(scrollHandle != nil)
  168.     {
  169.         /* Stash it in the document */
  170.         ((DocumentPeek)wind)->hScroll = scrollHandle;
  171.         
  172.         /* Now the vertical */
  173.         scrollHandle = NewControl(wind, &scrollRect, "\p", false, 
  174.                     min, min, max, scrollBarProc, 0);
  175.         if(scrollHandle != nil)
  176.         {
  177.             /* Stash it in the document */
  178.             ((DocumentPeek)wind)->vScroll = scrollHandle;
  179.             rslt = true;
  180.         }
  181.         else
  182.             DisposeControl(((DocumentPeek)wind)->hScroll);
  183.     }
  184.     if(rslt == true)
  185.     {
  186.         /* Set up the scroll bars for the current world */
  187.         AdjustScrollbars(wind, true);
  188.     }
  189.     return rslt;
  190. }
  191.  
  192. Boolean CheckMachine(void)
  193. {
  194.     short        err, rslt = true;
  195.     long        gestResult;
  196.  
  197. /*     Check the machine's capabilities to be sure we can run */
  198.     
  199.     /* Check for availability of Gestalt */
  200.     if(!TrapAvailable(_GestaltDispatch))
  201.         return false;
  202.     
  203.     /* Check for availability of GWorlds */
  204.     err = Gestalt(gestaltQuickdrawVersion, &gestResult);
  205.     if(err != noErr)
  206.         return false;
  207.     if(gestResult < 0x0200) /* no 32-bit QD */
  208.         return false;
  209.     
  210.     /* Check for availability of FSSpec file routines */
  211.     err = Gestalt(gestaltFSAttr, &gestResult);
  212.     if(err != noErr)
  213.         return false;
  214.     if((gestResult & (1 << gestaltHasFSSpecCalls)) == 0)
  215.         return false;
  216.     
  217.     /* Check for availability of Standard Get/Put routines */
  218.     err = Gestalt(gestaltStandardFileAttr, &gestResult);
  219.     if(err != noErr)
  220.         return false;
  221.     if((gestResult & (1 << gestaltStandardFile58)) == 0)
  222.         return false;
  223.  
  224.     /* Check for availability of FindFolder routine */
  225.     err = Gestalt(gestaltFindFolderAttr, &gestResult);
  226.     if(err != noErr)
  227.         return false;
  228.     if((gestResult & (1 << gestaltFindFolderPresent)) == 0)
  229.         return false;
  230.  
  231.     /* Check for availability of TempMem routines */
  232.     err = Gestalt(gestaltOSAttr, &gestResult);
  233.     if(err != noErr)
  234.         return false;
  235.     if((gestResult & (1 << gestaltTempMemSupport)) == 0)
  236.         return false;
  237.  
  238.     return true;
  239. }
  240.  
  241. /* This routine calculates a document window's title bar height and frame width, for
  242. later use in positioning and growing windows. (Did you know that GrowWindow() takes
  243. as limits the size of the window's strucRgn, but returns the size of its portRect?) */
  244.  
  245. void CalcWindowStats(void)
  246. {
  247.     WindowRecord    tempWind;
  248.     Rect            globalPortRect, strucRect, offScreenRect, deskRect;
  249.     
  250.     /* Make a small rectangle that is off the screen */
  251.     deskRect = (**GetGrayRgn()).rgnBBox;
  252.     SetRect(&offScreenRect, 0, 0, 32, 32);
  253.     OffsetRect(&offScreenRect, deskRect.left - 64, 0);
  254.     
  255.     /* Make a visible window that the user can't see. Only when a window is visible
  256.     can you find out its strucRgn dimensions */
  257.     NewWindow(&tempWind, &offScreenRect, "\p", true, documentProc, (Ptr)-1, false, 0);
  258.     
  259.     /* OK, we have a window. Now calculate the info we need. First we need
  260.     to globalize the portRect and get the strucRect of the window */
  261.     globalPortRect = ((GrafPtr)(&tempWind))->portRect;
  262.     SetPort(&tempWind);
  263.     LocalToGlobal(&topLeft(globalPortRect));
  264.     LocalToGlobal(&botRight(globalPortRect));
  265.     strucRect = (*(((WindowPeek)(&tempWind))->strucRgn))->rgnBBox;
  266.     
  267.     /* Calculate the window's title bar height. I'm not entirely sure why you need to
  268.     subtract 1 here, but Craig Prouse did it in his Window Zoom routine, and I'm sure
  269.     he had a good reason for it, so I'll do it too. */
  270.     gDocTitleHeight = globalPortRect.top - 1 - strucRect.top;
  271.     
  272.     /* Calculate the width of the windows right side frame */
  273.     gDocFrameWidth = strucRect.right - 1 - globalPortRect.right;
  274.     
  275.     /* All Done, kill the window */
  276.     CloseWindow(&tempWind);
  277. }
  278.     
  279.     
  280.     
  281.     
  282.     
  283.     
  284.     
  285.     
  286.     
  287.     
  288.     
  289.     
  290.     
  291.     
  292.